package com.tid.hbasees.repo;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tid.hbasees.model.InnerNetwork;
import com.tid.hbasees.model.OutsideNetwork;
import com.tid.hbasees.utils.hbase.HBaseHelper;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Scan;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Component
public class OutsideNetworkRepo {

    public static String TABLE_NAME = "outsideNetwork";
    public static String INDEX_TABLE_NAME_1 = TABLE_NAME + "_index1";
    public static String INDEX_TABLE_NAME_2 = TABLE_NAME + "_index2";
    public static String INDEX_TABLE_NAME_3 = TABLE_NAME + "_index3";

    @Resource(name = "hbaseHelper")
    private HBaseHelper hbaseHelper;

    private HTableInterface getTable() throws IOException {
        return hbaseHelper.getTable(TABLE_NAME);
    }

    public void save(List<OutsideNetwork> outsideNetworks) {

    }

    public List<OutsideNetwork> getOutsideNetworkByDeviceId(String deviceId, long startTime, long endTime) {
        Map<String, Scan> scans = Maps.newHashMap();
        scans.put(INDEX_TABLE_NAME_1, hbaseHelper.buildTypicalScan(deviceId, startTime, endTime));
        scans.put(INDEX_TABLE_NAME_2, hbaseHelper.buildTypicalScan(deviceId, startTime, endTime));
        try {
            return hbaseHelper.multiScan(scans, TABLE_NAME, OutsideNetwork.class);
        } catch (Exception e) {
            e.printStackTrace();
            return Lists.newLinkedList();
        }
    }

    public List<OutsideNetwork> getOutsideNetworkByNodeId(String nodeId, long startTime, long endTime) {
        Map<String, Scan> scans = Maps.newHashMap();
        scans.put(INDEX_TABLE_NAME_3, hbaseHelper.buildTypicalScan(nodeId, startTime, endTime));
        try {
            return hbaseHelper.multiScan(scans, TABLE_NAME, OutsideNetwork.class);
        } catch (Exception e) {
            e.printStackTrace();
            return Lists.newLinkedList();
        }
    }
}